﻿using System;
using System.Threading;
using CommonTypes.Client;
using Server.Model;

namespace Server.Services.Primary
{
    public class ProcessingRequests
    {
        internal readonly static Object _requestLock = new Object( );

        public static bool RegisterClient( String clientId, String clientLocation, ServerData serverData )
        {
            lock( _requestLock )
            {
                if( clientId.Length == 0 || clientLocation.Length == 0 )
                    return false;

                if( !serverData.Standalone )
                {
                    System.Console.WriteLine( "I'm sending the request to the secondary servers." );
                    PrimaryMergingManager.RegisterClientLocation( clientId, clientLocation, serverData );
                }

                System.Console.WriteLine( "Register request: " + clientId );

                if( serverData.UsersList.ContainsKey( clientId ) )
                    serverData.UsersList[ clientId ].Location = clientLocation;
                else
                {
                    User u = new User( clientId, clientLocation );
                    serverData.UsersList.Add( clientId, u );
                }

                serverData.ServerLogicalClock++;
                Monitor.Pulse( _requestLock );
            }

            return true;
        }

        public static bool UnregisterClient( String clientId, ServerData serverData )
        {
            lock( _requestLock )
            {
                if( clientId.Length == 0 )
                    return false;

                System.Console.WriteLine( "Unregister request: " + clientId );

                if( !serverData.Standalone )
                {
                    System.Console.WriteLine( "I'm sending the request to the secondary servers." );
                    PrimaryMergingManager.UnregisterClientLocation( clientId, serverData );
                }

                serverData.UsersList.Remove( clientId );
                serverData.ServerLogicalClock++;
                Monitor.Pulse( _requestLock );
            }

            return true;
        }

        public static void GenerateTicket( String clientId, DateTime clientNonce, ServerData serverData )
        {
            lock( _requestLock )
            {
                if( ( clientId == null ) || ( clientId.Length == 0 ) || !serverData.UsersList.ContainsKey( clientId ) )
                    return;

                if( !serverData.Standalone )
                {
                    System.Console.WriteLine( "I'm sending the request to the secondary servers." );
                    PrimaryMergingManager.GenerateTicket( clientId, serverData );
                }

                System.Console.WriteLine( "Generate ticket request: " + clientId );

                String clientLocation = serverData.UsersList[ clientId ].Location;
                IServerToClient client = ( IServerToClient )Activator.GetObject( typeof( IServerToClient ), clientLocation + "/ServerToClient" );

                client.TicketGenerationAnswer( serverData.TicketNumber++, clientNonce );
                serverData.ServerLogicalClock++;
                client = null;
                Monitor.Pulse( _requestLock );
            }
        }
    }
}